package com.homwee.factory.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;

import com.homwee.aidlservice.aidl.IKey;

import com.mediatek.twoworlds.factory.MtkTvFApiSystem;
import com.mediatek.twoworlds.factory.common.MtkTvFApiKeyManager;
import com.mediatek.twoworlds.factory.model.MtkTvFApiSysFactorySvcCmd_e;
import com.mediatek.twoworlds.factory.common.MtkTvFApiResultTypeBase;
import com.mediatek.twoworlds.factory.MtkTvFApiPeripheral;
import com.mediatek.twoworlds.factory.MtkTvFApiInformation;
import com.mediatek.twoworlds.tv.MtkTvCI;
import com.homwee.factory.util.Contants;
import java.io.File;
import java.io.FileInputStream;
import java.security.NoSuchAlgorithmException;
import java.io.IOException;
import java.util.ArrayList;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import com.homwee.listMenu.util.MenuItemData;
import java.security.MessageDigest;

import android.util.Log;

/**
 * Auth Peng.Xie Data 2019.07.08 Description 云端更新Key
 */

public class KeyService extends Service {

    private Binder binder = new IKey.Stub() {

        @Override
        public boolean widevineExist() throws RemoteException {
            return isWidevineKeyExist();
        }

        @Override
        public boolean netflixExist() throws RemoteException {
            return isNETFLIXKeyExist();
        }

        @Override
        public boolean macExist() throws RemoteException {
            return isMacExist();
        }

        @Override
        public boolean deviceIdExist() throws RemoteException {
            return isDeviceIDExist();
        }

        @Override
        public boolean hashExist() throws RemoteException {
            return isHashKeyExist();
        }

        @Override
        public boolean iniExist() throws RemoteException {
            return isIniExist();
        }

        @Override
        public boolean ciplusExist() throws RemoteException {
            return isDTVCIPlusKeyExist();
        }

        @Override
        public boolean hdcpExist() throws RemoteException {
            return isHDCPKey2XExist();
        }

        @Override
        public boolean hdcp14Exist() throws RemoteException {
            return isHDCPKey1XExist();
        }

        @Override
        public boolean keyMasterExist() throws RemoteException {
            return isKeyMasterExist();
        }

        @Override
        public boolean playReadyExist() throws RemoteException {
            return isPlayReadyExist();
        }
/*
        @Override
        public boolean playReady25Exist() throws RemoteException {
            return isPlayReady25Exist();
        }
*/
        @Override
        public boolean webPlateformCAExist() throws RemoteException {
            return isWebPlateformCAExist();
        }

        @Override
        public String getIniMac() throws RemoteException {
            return getIniMacState();
        }

        @Override
        public String getIniDeviceId() throws RemoteException {
            return getIniDeviceIdState();
        }

        @Override
        public String getProjectId() throws RemoteException {
            return KeyService.this.getProjectId();
        }

        @Override
        public String getEDID1FrameMD5() throws RemoteException {
            return KeyService.this.getMd5(Contants.KEY_PATH_EDID_1_FRAME);
        }

        @Override
        public String getEDID1HDRMD5() throws RemoteException {
            return KeyService.this.getMd5(Contants.KEY_PATH_EDID_1_HDR);
        }

        @Override
        public String getEDID2FrameMD5() throws RemoteException {
            return KeyService.this.getMd5(Contants.KEY_PATH_EDID_2_FRAME);
        }

        @Override
        public String getEDID2HDRMD5() throws RemoteException {
            return KeyService.this.getMd5(Contants.KEY_PATH_EDID_2_HDR);
        }

        @Override
        public String getEDID3FrameMD5() throws RemoteException {
            return KeyService.this.getMd5(Contants.KEY_PATH_EDID_3_FRAME);
        }

        @Override
        public String getEDID3HDRMD5() throws RemoteException {
            return KeyService.this.getMd5(Contants.KEY_PATH_EDID_3_HDR);
        }

        @Override
        public String getEDID4FrameMD5() throws RemoteException {
            return KeyService.this.getMd5(Contants.KEY_PATH_EDID_4_FRAME);
        }

        @Override
        public String getEDID4HDRMD5() throws RemoteException {
            return KeyService.this.getMd5(Contants.KEY_PATH_EDID_4_HDR);
        }

        @Override
        public String getHBBTVEnabled() throws RemoteException {
            return isHBBTVEnabled();
        }

        @Override
        public int getESNKeyConfig() throws RemoteException {
            return KeyService.this.getESNKeyConfig();
        }

        @Override
        public void setMac(String mac) throws RemoteException {
            setMAC(mac);
        }

        @Override
        public void setDeviceId(byte[] deviceId) throws RemoteException {
            setDeviceIDByBytes(deviceId);
        }

        @Override
        public void setWideVine(byte[] wideVine) throws RemoteException {
            Log.d("setWideVine", "1");
            setWidevineKey(wideVine, Contants.KEY_PATH_WINDVINDE, Contants.KEY_PATH_WINDVINDE_DIR);
        }

        @Override
        public void setHash(byte[] hash) throws RemoteException {
            Log.d("setHash", "1");
            setWidevineKey(hash, Contants.KEY_PATH_HASH_KEY);
        }

        @Override
        public void setNetflixKPEKPH(byte[] netflixKPEKPH) throws RemoteException {
            Log.d("setNetflixKPEKPH", "1");
            setNetflixKPEKPHByByte(netflixKPEKPH);
        }

        @Override
        public void setNetflixESNID(byte[] netflixESNID) throws RemoteException {
            Log.d("setNetflixESNID", "1");
            setNetflixESNIDByByte(netflixESNID);
        }

        @Override
        public void setEDID(byte[] edid, String path) throws RemoteException {
            Log.d("setEDID", "path: " + path);
            setWidevineKey(edid, path);
        }

        @Override
        public void setCIPlus(byte[] ciPlus) throws RemoteException {
            Log.d("setCiPlus", "1");
            setCIPlusByByte(ciPlus);
        }

        @Override
        public void setHDCP(byte[] hdcp) throws RemoteException {
            Log.d("setHdcp", "1");
            setHDCP2ByByte(hdcp);
        }

        @Override
        public void setHDCP14(byte[] hdcp14) throws RemoteException {
            Log.d("setHdcp14", "1");
            setHDCP14ByByte(hdcp14);
        }

        @Override
        public void setKeyMaster(byte[] keyMaster) throws RemoteException {
            Log.d("setKeyMaster", "1");
            setKeyMasterByByte(keyMaster);
        }

        @Override
        public void setPlayReady1(byte[] playReady1) throws RemoteException {
            Log.d("setPlayReady1", "playReady1: " + playReady1);
            setPlayReady1ByByte(playReady1);
        }

        @Override
        public void setPlayReady2(byte[] playReady2) throws RemoteException {
            Log.d("setPlayReady2", "playReady2: " + playReady2);
            setPlayReady2ByByte(playReady2);
        }
/*
        @Override
        public void setPlayReady25_1(byte[] playReady25_1) throws RemoteException {
            Log.d("setPlayReady25_1", "playReady25_1: " + playReady25_1);
            setPlayReady25_1ByByte(playReady25_1);
        }

        @Override
        public void setPlayReady25_2(byte[] playReady25_2) throws RemoteException {
            Log.d("setPlayReady25_2", "playReady25_2: " + playReady25_2);
            setPlayReady25_2ByByte(playReady25_2);
        }
*/
        @Override
        public void setWebPlateformCA(byte[] webPlateformCA) throws RemoteException {
            Log.d("setWebPlateformCA", "webPlateformCA: " + webPlateformCA);
            setWebPlateformCAByByte(webPlateformCA);
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public boolean isHDCPKey1XExist() {
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        if (mtkTvFApiSystem != null) {
            return mtkTvFApiSystem.checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_HDCP14);
        }
        return false;
    }

    public boolean isHDCPKey2XExist() {
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        if (mtkTvFApiSystem != null) {
            return mtkTvFApiSystem.isFileExist(Contants.KEY_PATH_HDCP2);
        }
        return false;
    }

    public boolean isDTVCIPlusKeyExist() {
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        if (mtkTvFApiSystem != null) {
            return mtkTvFApiSystem.checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_CIPLUS);
        }
        return false;
    }

    private boolean isWidevineKeyExist() {
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        if (mtkTvFApiSystem != null) {
            return mtkTvFApiSystem.checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_WVCENC);
        }
        return false;
    }

    private boolean isNETFLIXKeyExist() {
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        if (mtkTvFApiSystem != null) {
            return mtkTvFApiSystem
                    .checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_NETFLIX50);
        }
        return false;
    }

    private boolean isKeyMasterExist() {
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        if (mtkTvFApiSystem != null) {
            return mtkTvFApiSystem
                    .checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_KEYMASTER);
        }
        return false;
    }

    private boolean isPlayReadyExist() {
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        if (mtkTvFApiSystem != null) {
            return mtkTvFApiSystem
                    .checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADY30);
        }
        return false;
    }

    public boolean isPlayReady25Exist() {
        MtkTvFApiSystem mMtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        if (mMtkTvFApiSystem != null) {
            return mMtkTvFApiSystem
                    .checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADY25);
        }
        return false;
    }

    private boolean isHashKeyExist() {
        return MtkTvFApiSystem.getInstance().isFileExist(Contants.KEY_PATH_HASH_KEY);
    }

    private boolean isProjectIdExist() {
        return MtkTvFApiSystem.getInstance().isFileExist(Contants.PATH_PROJECT_INI);
    }

    private boolean isDeviceIDExist() {
        return !"null".equalsIgnoreCase(getDeviceID());
    }

    private boolean isMacExist() {
        return !"null".equalsIgnoreCase(getMAC());
    }

    private boolean isIniExist() {
        return MtkTvFApiSystem.getInstance().isFileExist(Contants.KEYSTATUS_INI_FILEPATH);
    }

    private String getIniMacState() {
        return MtkTvFApiInformation.getInstance().getIniParameter(Contants.KEYSTATUS_INI_FILEPATH,
                Contants.KEYSTATUS_MAC);
    }

    private String getIniDeviceIdState() {
        return MtkTvFApiInformation.getInstance().getIniParameter(Contants.KEYSTATUS_INI_FILEPATH,
                Contants.KEYSTATUS_DEVICEID);
    }

    private String getDeviceID() {
        String devIDStr = MtkTvFApiSystem.getInstance().getEmmcEnvVar("serialno");
        if (devIDStr == null || devIDStr.equalsIgnoreCase("")) {
            devIDStr = "null";
        }
        return devIDStr;

    }

    private String getMAC() {
        String hardWareAddress = MtkTvFApiSystem.getInstance().getEmmcEnvVar("macaddr");
        if (hardWareAddress == null || hardWareAddress.equalsIgnoreCase("")) {
            hardWareAddress = "null";
        }
        return hardWareAddress;
    }

    private String getProjectId() {
        String name = MtkTvFApiInformation.getInstance().getIniParameter(Contants.PATH_PROJECT_INI,
                Contants.FACTORY_PROJECT_ID);

        Log.d("getProjectId =", name);
        return name;
    }

    private boolean isWebPlateformCAExist() {
        return MtkTvFApiSystem.getInstance().isFileExist(Contants.KEY_PATH_WEBPLATFORM_CA);
    }

    private String isHBBTVEnabled() {
        return MtkTvFApiInformation.getInstance().getIniParameter(Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_HBBTV_ENABLE);
    }

    /**
     * MD5
     * 
     * @return
     */
    private String getMd5(String path) {
        File file = new File(path);
        String md5 = getFileMD5(file);
        return md5;
    }

    private String getFileMD5(File file) {
        if (!file.isFile()) {
            return "";
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte[] buffer = null;
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            int length = in.available();
            buffer = new byte[length];
            while ((len = in.read(buffer, 0, length)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        return bytesToHexString(digest.digest());
    }

    /**
     * byte to string.
     */
    private String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    // MAC

    private boolean setMAC(String mac) {
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        int mode = -1;
        if (mtkTvFApiSystem != null) {
            mode = mtkTvFApiSystem.setEmmcEnvVar("macaddr", mac);
        }
        if (mode > -1) {
            int command = MtkTvFApiSysFactorySvcCmd_e.E_MTK_TV_FAPI_SYS_FACTORY_CMD_SYS_CALL;
            String parameter = "ifconfig eth0 down";
            MtkTvFApiSystem.getInstance().sendCmdToFactorySvc(command, parameter);
            String parameter_1 = "ifconfig eth0 hw ether " + mac;
            MtkTvFApiSystem.getInstance().sendCmdToFactorySvc(command, parameter_1);
            String parameter_2 = "ifconfig eth0 up";
            MtkTvFApiSystem.getInstance().sendCmdToFactorySvc(command, parameter_2);
        }
        return (mode > -1);
    }

    // DeviceID

    private boolean setDeviceIDByBytes(byte[] bytes) {
        String deviceIdStr = new String(bytes);

        deviceIdStr = deviceIdStr.trim().replaceAll(" ", "");

        Log.i("DeviceID: ", deviceIdStr);

        return setDeviceID(deviceIdStr);
    }

    private boolean setDeviceID(String deviceID) {
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        int mode = -1;
        if (mtkTvFApiSystem != null) {
            mode = mtkTvFApiSystem.setEmmcEnvVar("serialno", deviceID);
        }
        return (mode > -1);
    }

    // WideVine
    private boolean setWidevineKey(byte[] bytes, String path) {
        return this.setWidevineKey(bytes, path, null);
    }

    private boolean setWidevineKey(byte[] bytes, String path, String dir) {
        Log.i("setWidevineKey", "1");
        boolean copyRt = writeFileByte(bytes, path);
        Log.i("setWidevineKey.copyRt", "" + copyRt);
        if (copyRt) {
            changeFileMode(0644, path);
            if (null != dir) {
                changeFileMode(0755, dir);
            }
        }
        Log.i("setWidevineKey", "2");
        int encryptRt = MtkTvFApiSystem.getInstance()
                .encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_WVCENC, true);
        Log.i("setWidevineKey.encryptRt", "" + encryptRt);
        return copyRt;
    }

    // netflix KPEKPH
    private boolean setNetflixKPEKPHByByte(byte[] bytes) {
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_NETFLIX_KEY);
        if (copyRt) {
            changeFileMode(0644, Contants.KEY_PATH_NETFLIX_KEY);
        }
        int encryptRt = MtkTvFApiSystem.getInstance()
                .encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_NETFLIX50, true);
        return copyRt;
    }

    // netflix ESNID
    private boolean setNetflixESNIDByByte(byte[] bytes) {
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_NETFLIX_ESN_KEY);
        if (copyRt) {
            changeFileMode(0644, Contants.KEY_PATH_NETFLIX_ESN_KEY);
            changeFileMode(0755, Contants.KEY_PATH_NETFLIX_DIR);
        }
        return copyRt;
    }

    // hdcp2
    private void setHDCP2ByByte(byte[] bytes) {
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_HDCP2);
        if (copyRt) {
            changeFileMode(0644, Contants.KEY_PATH_HDCP2);
            reloadNeedKeys();
        }
    }

    // hdcp14
    private void setHDCP14ByByte(byte[] bytes) {
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_HDCP14);
        if (copyRt) {
            changeFileMode(0644, Contants.KEY_PATH_HDCP14);
            reloadNeedKeys();
        }
    }

    // playReady 1
    private boolean setPlayReady1ByByte(byte[] bytes) {
        Log.i("setPlayReady1", "bytes: " + bytes);
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_PLAYREADY_1);
        Log.i("setPlayReady1", "copyRt: " + copyRt);
        if (copyRt) {
            changeFileMode(0666, Contants.KEY_PATH_PLAYREADY_1);
            changeFileMode(0755, Contants.KEY_PATH_PLAYREADY_DIR);
        }
        int encryptRt = MtkTvFApiSystem.getInstance()
                .encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADY30, true);
        return copyRt;
    }

    // playReady 2
    private boolean setPlayReady2ByByte(byte[] bytes) {
        Log.i("setPlayReady2", "bytes: " + bytes);
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_PLAYREADY_2);
        Log.i("setPlayReady2", "copyRt: " + copyRt);
        if (copyRt) {
            changeFileMode(0666, Contants.KEY_PATH_PLAYREADY_2);
            changeFileMode(0755, Contants.KEY_PATH_PLAYREADY_DIR);
        }
        int encryptRt = MtkTvFApiSystem.getInstance()
                .encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADYPUB30, false);
        return copyRt;
    }
/*
    // playReady 25 1
    private boolean setPlayReady25_1ByByte(byte[] bytes) {
        Log.i("setPlayReady1", "bytes: " + bytes);
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_PLAYREADY25_1);
        Log.i("setPlayReady1", "copyRt: " + copyRt);
        if (copyRt) {
            changeFileMode(0666, Contants.KEY_PATH_PLAYREADY25_1);
            changeFileMode(0755,Contants.KEY_PATH_PLAYREADY25_DIR);
        }
        int encryptRt = MtkTvFApiSystem.getInstance()
                .encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADY25, true);
        return copyRt;
    }

    // playReady 25 2
    private boolean setPlayReady25_2ByByte(byte[] bytes) {
        Log.i("setPlayReady2", "bytes: " + bytes);
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_PLAYREADY25_2);
        Log.i("setPlayReady2", "copyRt: " + copyRt);
        if (copyRt) {
            changeFileMode(0666, Contants.KEY_PATH_PLAYREADY25_2);
            changeFileMode(0755, Contants.KEY_PATH_PLAYREADY25_DIR);
        }
        int encryptRt = MtkTvFApiSystem.getInstance()
                .encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_PLAYREADYPUB25, false);
        return copyRt;
    }
*/
    //  setWebPlateformCAByByte
    private boolean setWebPlateformCAByByte(byte[] bytes) {
        Log.i("setWebPlateformCAByByte", "bytes: " + bytes);
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_WEBPLATFORM_CA);
        Log.i("setWebPlateformCAByByte", "copyRt: " + copyRt);
        if (copyRt) {
            changeFileMode(0775,Contants.KEY_PATH_WEBPLATFORM_CA);
			changeFileMode(0755,Contants.KEY_PATH_WEBPLATFORM_CA_DIR);
        }
        return copyRt;
    }

    // ci plus
    private boolean setCIPlusByByte(byte[] bytes) {
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_CIPLUS);
        int encryptRt = MtkTvCI.getInstance(0).updateCIKeyWithPathEx(Contants.KEY_PATH_CIPLUS, 0);
        return copyRt && (encryptRt > -1);
    }

    // keyMaster
    private boolean setKeyMasterByByte(byte[] bytes) {
        boolean copyRt = writeFileByte(bytes, Contants.KEY_PATH_KEYMASTER);
        int encryptRt = MtkTvFApiSystem.getInstance()
                .encryptSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_KEYMASTER, true);
        return copyRt;
    }

    //  ESNKeyConfig
    private int getESNKeyConfig() {
        //  Contants.FACTORY_ESN_KEY_CONFIG = "FACTORY_CONFIG:ESNKEYCONFIG";
        int config = 0;
		config = MtkTvFApiInformation
				.getInstance()
				.getIniParameter(Contants.PATH_FACTORYINI,
						Contants.FACTORY_ESN_KEY_CONFIG).equalsIgnoreCase("1") ? 1 : 0;
		return config;
    }

    private boolean changeFileMode(int mode, String path) {
        Log.i("changeFileMode", "1");
        MtkTvFApiSystem mtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        int ret = -1;
        ret = mtkTvFApiSystem.changeFileMode(path, mode);
        Log.i("changeFileMode", "2");
        return false;
    }

    private boolean writeFileByte(byte[] bytes, String src) {
        String TAG = "writeFileByte";
        Log.d(TAG, "[System] WriteFileByte()");
        int actualRet = MtkTvFApiResultTypeBase.MTKTV_FAPI_RET_FAIL;

        ArrayList<Byte> bootargsArrayList = new ArrayList<>();
        for (byte b : bytes) {
            bootargsArrayList.add(b);
        }

        /*
         * // 1st Test: writeFile() isAppendMode is true, addNewLine is true String
         * filePath1st = "/vendor/tvcertificate/test.bin"; actualRet =
         * MtkTvFApiSystem.getInstance().createFile(filePath1st);
         * 
         * Log.d(TAG, "Check the file status"); actual =
         * MtkTvFApiSystem.getInstance().isFileExist(filePath1st);
         * 
         * Log.d(TAG, "Write File: " + filePath1st); String openFileMode = "ab";
         * actualRet = MtkTvFApiSystem.getInstance().writeFileByte(filePath1st,
         * bootargsArrayList, openFileMode);
         * 
         * int fileMode = 0755; //This fileMode is used for octal format actualRet =
         * MtkTvFApiSystem.getInstance().changeFileMode(filePath1st, fileMode);
         * 
         */

        // 2nd Test: writeFile() isAppendMode is false, addNewLine is false
        boolean actual = MtkTvFApiSystem.getInstance().isFileExist(src);
        if (actual) {
            MtkTvFApiSystem.getInstance().removeFile(src);
        }

        actualRet = MtkTvFApiSystem.getInstance().createFile(src);

        actual = MtkTvFApiSystem.getInstance().isFileExist(src);

        String openFileMode = "wb";
        actualRet = MtkTvFApiSystem.getInstance().writeFileByte(src, bootargsArrayList, openFileMode);

        return actual;
    }

    // reloadNeedKeys
    public boolean reloadNeedKeys() {
        MtkTvFApiSystem mMtkTvFApiSystem = MtkTvFApiSystem.getInstance();
        int ret = 0;
        if (mMtkTvFApiSystem.checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_HDCP14)
                || mMtkTvFApiSystem.checkSystemKey(MtkTvFApiKeyManager.EnumSystemKeyType.E_MTK_FAPI_SYSTEM_KEY_HDCP2)) {
            ret = MtkTvFApiPeripheral.getInstance().reloadHdcpKey(Contants.KEY_PATH_HDCP14, Contants.KEY_PATH_HDCP2);
        }
        return ret == 0 ? true : false;
    }
}
